package net.tarine.gastronomi.client.tables;

import java.util.ArrayList;
import java.util.List;

import net.tarine.gastronomi.client.IRefreshable;
import net.tarine.gastronomi.client.UiSingleton;
import net.tarine.gastronomi.client.forms.GasUserPopUp;
import net.tarine.gastronomi.client.services.GasService;
import net.tarine.gastronomi.client.services.GasServiceAsync;
import net.tarine.gastronomi.shared.AppConst;
import net.tarine.gastronomi.shared.Role;
import net.tarine.gastronomi.shared.to.GasUserTo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Image;

public class GasUserTable extends PagingTable<GasUserTo> implements IRefreshable {
	
	private AsyncCallback<List<GasUserTo>> callback = new AsyncCallback<List<GasUserTo>>() {
		@Override
		public void onFailure(Throwable caught) {
			UiSingleton.get().addError(caught);
			setTableRows(new ArrayList<GasUserTo>());
		}
		@Override
		public void onSuccess(List<GasUserTo> result) {
			setTableRows(result);
		}
	};
	
	public GasUserTable(DataModel<GasUserTo> model) {
		super(model);
		drawPage(0);
	}

	@Override
	protected void drawPage(int page) {
		getInnerTable().removeAllRows();
		getInnerTable().setHTML(0, 0, "<img src='img/ajax-loader-small.gif' style='vertical-align:middle' />&nbsp;" +
				"Caricamento in corso...");
		getModel().find(page*AppConst.PAGING_ROWS,
				AppConst.PAGING_ROWS,
				callback);
	}
	
	@Override
	public void refresh() {
		drawPage(0);
	}
	
	@Override
	protected void addTableRow(int rowNum, GasUserTo rowObj) {
		final GasUserTo rowFinal = rowObj;
		final GasUserTable utentiTable = this;
		// Set the data in the current row
		Anchor rowLink = new Anchor();
		rowLink.setText(rowObj.getNickname());
		rowLink.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent event) {
				new GasUserPopUp(rowFinal.getId(), utentiTable);
			}
		});
		getInnerTable().setWidget(rowNum, 0, rowLink);
		getInnerTable().setHTML(rowNum, 1, rowObj.getEmail());
		String role = null;
		for (Role r:Role.values()) {
			if (r.getId()==rowObj.getRoleId()) role = r.getDescription();
		}
		getInnerTable().setHTML(rowNum, 2, role);
		Image trashImg = new Image("img/trash.png");
		trashImg.setTitle("elimina");
		trashImg.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				confirmAndDelete(rowFinal.getId());
			}
		});
		getInnerTable().setWidget(rowNum, 3, trashImg);
		rowNum += 1;
	}

	@Override
	protected void addHeader() {
		// Set the data in the current row
		getInnerTable().setHTML(0, 0, "Nome utente");
		getInnerTable().setHTML(0, 1, "Email");
		getInnerTable().setHTML(0, 2, "Ruolo");
	}
	
	private void confirmAndDelete(String idUtente) {
		boolean confirm = Window.confirm("Vuoi veramente cancellare l'utente?");
		if (confirm) {
			delete(idUtente);
		}
	}
	
	public void delete(String gasUserId) {
		GasServiceAsync gasUserService = GWT.create(GasService.class);
		gasUserService.deleteGasUser(gasUserId, callback);
	}
	
	
	
	
	//Inner classes
	
	
	
	public static class GasUserModel implements DataModel<GasUserTo> {
		private final GasServiceAsync gasUserService = GWT.create(GasService.class);
		
		public GasUserModel() {	}

		@Override
		public void find(int offset, int pageSize, AsyncCallback<List<GasUserTo>> callback) {
			gasUserService.findGasUsers(offset, pageSize, callback);
		}
	}
	
}
